package com.example.sunmiv1.vm

import android.app.AlertDialog
import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.util.Log
import android.widget.DatePicker
import com.example.sunmiv1.activity.UpdateInfoActivity
import com.example.sunmiv1.entity.LoginEntity
import com.example.sunmiv1.entity.MemberInfoBean
import com.example.sunmiv1.room.UserVipDaoManager
import com.example.sunmiv1.room.entity.UserVipEntity
import com.example.sunmiv1.utils.GsonTools
import com.liuwan.demo.datepicker.CustomDatePicker
import kotlinx.android.synthetic.main.activity_update_info.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.util.Calendar


class UpdateInfoViewModel(private val mActivity:UpdateInfoActivity,private val mLogin:LoginEntity){
    private var mDatePicker:CustomDatePicker?=null
    private var mMemberInfoBean:MemberInfoBean?=null
    private val TAG=javaClass.simpleName
    fun init(data: MemberInfoBean){
        mMemberInfoBean=data
        Log.e(TAG, "init: data=${GsonTools.beanToJson(data)}")
        GlobalScope.launch(Dispatchers.IO){
            val list=UserVipDaoManager.findUserId(data.vip?.id?:"")
            GlobalScope.launch(Dispatchers.Main){
                initView(list.firstOrNull())
            }
        }
        with(mActivity){
            updateInfo_birthday.setOnClickListener {
                val calendar = Calendar.getInstance()
                // 綁定監聽器(How the parent is notified that the date is set.)
                DatePickerDialog(
                    mActivity,
                    AlertDialog.THEME_HOLO_LIGHT,
                    OnDateSetListener { view1: DatePicker?, year: Int, monthOfYear: Int, dayOfMonth: Int ->
                        // 此處得到選擇的時間，可以進行你想要的操作
                        updateInfo_birthday.text=year.toString() + "-" + (monthOfYear + 1) + "-" + dayOfMonth
                    },
                    calendar[Calendar.YEAR],
                    calendar[Calendar.MONTH],
                    calendar[Calendar.DAY_OF_MONTH]
                ).show()
            }
        }
    }
    fun saveEntity(){
        with(mActivity){
            if(mMemberInfoBean?.vip?.id?.isNotBlank()!=true){
                mActivity.saveBt()
                return
            }
            GlobalScope.launch(Dispatchers.IO){
                val list=UserVipDaoManager.findUserId(mMemberInfoBean?.vip?.id?:"")
                val first=list.firstOrNull()?:UserVipEntity()
                first.also {
                    it.userId=mMemberInfoBean?.vip?.id?:""
                    it.birthday=updateInfo_birthday.text.toString()
                    it.address1=updateInfo_address1.editableText.toString()
                    it.address2=updateInfo_address2.editableText.toString()
                    it.address3=updateInfo_address3.editableText.toString()
                    it.postCode=updateInfo_postcode.editableText.toString()
                    it.travel=updateInfo_travel.editableText.toString()
                    it.email=updateInfo_email.editableText.toString()
                }
                if(list.isNotEmpty()){
                    UserVipDaoManager.update(first)
                }else{
                    UserVipDaoManager.add(first)
                }
                GlobalScope.launch(Dispatchers.Main){
                    mActivity.finish()
                }
            }
        }
    }
    private fun initView(entity: UserVipEntity?){
        with(mActivity){
            updateInfo_birthday.text=entity?.birthday?:""
            updateInfo_address1.setText(entity?.address1?:"")
            updateInfo_address2.setText(entity?.address2?:"")
            updateInfo_address3.setText(entity?.address3?:"")
            updateInfo_postcode.setText(entity?.postCode?:"")
            updateInfo_travel.setText(entity?.travel?:"")
            updateInfo_email.setText(entity?.email?:"")
        }
    }
}